<?php


namespace app\common\command;


use app\common\cachemodel\OrdersLib as OrdersLibCacheModel;
use app\pay\controller\Aliquery;
use app\pay\controller\Weixin;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class RepairCoin extends Command
{
    /**
     * 命令配置
     */
    protected function configure()
    {
        $this->setName('repair:coin')->setDescription('修复准币状态');
    }

    protected function execute(Input $input, Output $output)
    {
        $order_id = '';
        $countNum = 0;
        try
        {
            $info = Db::query('SELECT uid,order_id,trade_type,account,total_amount FROM wechat_backup_07_18_14_43_00.zhunle_user_account_record WHERE `status`=0 AND `order_type`=9 AND create_time > 1651766400 AND create_time < 1652112000;');
            $AliPay = new Aliquery();
            $WxPay = new Weixin();
            foreach ($info as $i)
            {
                $order_id = $i['order_id'];
                $isUpdate = false;
                if ($i['trade_type'] === 1)
                {
                    $result = json_decode(json_encode($AliPay->query(['out_trade_no' => $i['order_id'], 'trade_no' => ''], $i['account'])), true)['alipay_trade_query_response'];
                    if (isset($result['trade_status']) && $result['trade_status'] === 'TRADE_SUCCESS')
                    {
                        $isUpdate = true;
                    }
                } else if ($i['trade_type'] === 2)
                {
                    $result2 = $WxPay->query($i['order_id'], $i['trade_type'], $i['account']);
                    if (isset($result2['trade_state']) && $result2['trade_state'] === 'SUCCESS')
                    {
                        $isUpdate = true;
                    }
                } else
                {
                    print_r('时间【' . date('Y-m-d H:i:s') . "】准币订单【{$i['order_id']}】平台查询不到" . PHP_EOL);
                }
                if ($isUpdate === true)
                {
                    OrdersLibCacheModel::UpdateCoin($i['order_id'], 14, $i['total_amount'], $i['uid'], 1, $i['trade_type']);
                    print_r('时间【' . date('Y-m-d H:i:s') . "】准币订单【{$i['order_id']}】完成" . PHP_EOL);
                    $countNum++;
                }
            }
        }
        catch (\Exception $e)
        {
            print_r('时间【' . date('Y-m-d H:i:s') . "】准币订单【{$order_id}】 失败信息【{$e->getMessage()}】" . PHP_EOL);
        }
        print_r('时间【' . date('Y-m-d H:i:s') . "】准币订单状态修改成功数量【{$countNum}】" . PHP_EOL);
    }
}